package com.eascs.crm.nsEditDetail.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.nsEditDetail.service.SponsorInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

@Controller
@RequestMapping("/sponsorInfo/rest")
public class SponsorInfoRestController extends BaseRestController {
	private final Logger logger = LoggerFactory.getLogger(SponsorInfoRestController.class);
	@Autowired
	private SponsorInfoService sponsorInfoService;
	@Reference
	private CustomerService customerService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	
	
	@RequestMapping(value = "/add-view")
	public HtmlRestResult queryIndex(String loanapplyNo) {
		ViewContext.put("loanapplyNo", loanapplyNo);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/sponsor_info");
	}
	
	
	
	@RequestMapping("/save")
	public BaseRestResult<String> saveInfoSave(CustomerEntity entity) {
		String customerName = entity.getCustomerFamilyName() + entity.getCustomerGivenName();
		entity.setCustomerName(customerName);
		com.eascs.crm.common.vo.ModelResult<CustomerEntity> modelResult=customerService.addCustomer(entity);
		if(!modelResult.isSuccess()){
			return BaseRestResult.error(modelResult.getErrMsg());
		}
		return BaseRestResult.info("新增成功").andCloseActionDialog();
	}
	
	
	/*@RequestMapping("/save")
	public BaseRestResult<String> saveInfoSave(O2oLoanUserEntity entity) {
		
		String documentNo = entity.getDocumentNo();
		O2oLoanUserEntity loanUser = sponsorInfoService.queryEntityByDocumentNo(documentNo);
		if(loanUser != null ){
			 return BaseRestResult.error("新增担保人已经存在，不能重复添加");
		}
		String userFullName = entity.getUserSurName() + entity.getUserName();
		entity.setUserFullName(userFullName);
		ModelResult<O2oLoanUserEntity> modelResult = sponsorInfoService.saveEntity(entity);
		if (!modelResult.isSuccess()) {
			 return BaseRestResult.error("新增失败");
		}
		return BaseRestResult.info("新增成功").andCloseActionDialog();
	}*/
	
	@RequestMapping(value = "/sponsor_detail")
	public HtmlRestResult sponsorDetail(String loanapplyNo,String sponsorId,String flag, String approvalStatus) {
		O2oLoanUserEntity sponsorEntity= o2oLoanUserService.findSponsorById(sponsorId, loanapplyNo);
		O2oLoanUserEntity sponsorEntity_sensitive= o2oLoanUserService.findSponsorById_sensitive(sponsorId, loanapplyNo);//脱敏
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("sponsorEntity", sponsorEntity);
		ViewContext.put("sponsorEntity_sensitive", sponsorEntity_sensitive);
		ViewContext.put("flag", flag);
		ViewContext.put("approvalStatus", approvalStatus);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/sponsor_detail");
	}
	
}
